<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// 微信用户表中的信息在$this->userinfo
// 用户newmember表的信息在$this->newmember中
// 用户角色表的信息在$this->role中
namespace Home\Controller;
use Admin\Api\RoleApi;
use Bbs\Api\BbsMemberApi;
use Shop\Api\BannersApi;
use Shop\Api\CategoryApi;
use Shop\Api\ProductMemberPriceApi;
use Think\Controller;

use Shop\Api\ProductApi;
use Shop\Api\ProductFaqApi;
use Shop\Api\ProductGroupApi;
use Shop\Api\ProductSkuApi;
use Weixin\Api\WxuserApi;

class ClassifyController extends HomeController {

    /*
     * 分类主页
     * */
    public function index(){
        //农产品分类id
        $map['level'] = 2;
        $list = apiCall(CategoryApi::QUERY_NO_PAGING,array($map));
        if($list['status']){
            $this->assign('list',$list['info']);
        }
        $this->display();
    }

    /*
     * 分类商品列表
     * */
    public function query(){
        $name = I('name','');

        if(!empty($name)){
            $map['name'] = array('like','%'.$name.'%');
            $this->assign('name',$name);
        }

        $cate_id = I('cate_id','');

        if(!empty($cate_id)){
            $map['cate_id'] = $cate_id;
        }


        $this->assign('cate_id',$map['cate_id']);

        /*
         * 采用异步加载
         * */
        $page = I('page');
        if($page <= 1){
            //获取幻灯片

            $info = apiCall(ProductApi::QUERY_WITH_COUNT,array($map,array('curpage'=>1,'size'=>6)));

            if($info['status']){
                foreach($info['info']['list'] as &$v){
                    $skumap['product_id'] = $v['id'];
                    $pricesku = apiCall(ProductSkuApi::GET_INFO,array($skumap,'price asc'));
                    if($pricesku['status']){
                        $v['ori_price'] = $pricesku['info']['ori_price'];

                        $v['price'] = ceil($pricesku['info']['price']*$this->role['discount']);
                        $v['real_price'] = ceil($pricesku['info']['price']);
                        $v['integral'] = $v['price']*$this->role['integral_multiple'];
                    }
                }
            }
            $this->assign('list',$info['info']['list']);
            $this->display();
        }else{
            $info = apiCall(ProductApi::QUERY_WITH_COUNT,array($map,array('curpage'=>$page,'size'=>6)));

            if($info['status']&&count($info['info']['list'])>=1){
                foreach($info['info']['list'] as &$v){
                    $skumap['product_id'] = $v['id'];
                    $pricesku = apiCall(ProductSkuApi::GET_INFO,array($skumap,'price asc'));
                    if($pricesku['status']){
                        $v['ori_price'] = $pricesku['info']['ori_price'];
                        $v['price'] = ceil($pricesku['info']['price']*$this->role['discount']);
                        $v['real_price'] = ceil($pricesku['info']['price']);
                        $v['integral'] = $v['price']*$this->role['integral_multiple'];
                    }
                }
                $this->ajaxReturn(array('status'=>true, 'info'=>$info['info']['list']));
            }else{
                $this->ajaxReturn(array('status'=>false));
            }

        }
    }




    /*
     * 定制商品列表
     * */
    public function private_query(){


        $map['made_order'] =array('gt','0');

        $page = I('page');
        if($page <= 1){
            //获取幻灯片
            $info = apiCall(ProductApi::QUERY_WITH_COUNT,array($map,array('curpage'=>1,'size'=>6)));
            if($info['status']){
                foreach($info['info']['list'] as &$v){
                    $skumap['product_id'] = $v['id'];
                    $pricesku = apiCall(ProductSkuApi::GET_INFO,array($skumap,'price asc'));
                    if($pricesku['status']){
                        $v['ori_price'] = $pricesku['info']['ori_price'];

                        $v['price'] = ceil($pricesku['info']['price']);

                        $v['integral'] = $v['price']*$this->role['integral_multiple'];
                    }
                }
           }
            $this->assign('list',$info['info']['list']);
            $this->display();
        }else{
            $info = apiCall(ProductApi::QUERY_WITH_COUNT,array($map,array('curpage'=>$page,'size'=>6)));

            if($info['status']&&count($info['info']['list'])>=1){
                foreach($info['info']['list'] as &$v){
                    $skumap['product_id'] = $v['id'];
                    $pricesku = apiCall(ProductSkuApi::GET_INFO,array($skumap,'price asc'));
                    if($pricesku['status']){
                        $v['ori_price'] = $pricesku['info']['ori_price'];
                        $v['price'] = ceil($pricesku['info']['price']);
                        $v['integral'] = $v['price']*$this->role['integral_multiple'];
                    }
                }
                $this->ajaxReturn(array('status'=>true, 'info'=>$info['info']['list']));
            }else{
                $this->ajaxReturn(array('status'=>false));
            }

        }
    }
}